package com.qiangzhi.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qiangzhi.constant.DefaultDataConstant;
import com.qiangzhi.constant.StatusConstant;
import com.qiangzhi.pojo.dto.UserDTO;
import com.qiangzhi.pojo.dto.UserPageQueryDTO;
import com.qiangzhi.mapper.ManageMapper;
import com.qiangzhi.pojo.po.UserPO;
import com.qiangzhi.result.PageResult;
import com.qiangzhi.service.ManageService;
import com.qiangzhi.pojo.vo.UserVO;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class ManageServiceImpl implements ManageService {

    @Autowired
    private ManageMapper manageMapper;

    /**
     * 新增教师和学生账号
     *
     * @param userDTO
     */
    @Transactional
    public void insertUser(UserDTO userDTO) {
        UserPO userPO = new UserPO();
        BeanUtils.copyProperties(userDTO, userPO);

        // 设置账号当前的状态 默认启用状态 1表示启用 0表示锁定
        userPO.setStatus(StatusConstant.ENABLE);

        // 设置密码 默认密码123456
        userPO.setPassword(DigestUtils.md5DigestAsHex(DefaultDataConstant.DEFAULT_PASSWORD.getBytes()));

        // info和detail表都要新增数据
        manageMapper.insertUser(userPO);
        manageMapper.insertUserDetail(userPO.getId(), userPO.getIdentify());
    }

    /**
     * 教师和学生分页查询
     *
     * @param userPageQueryDTO
     * @return
     */
    public PageResult page(UserPageQueryDTO userPageQueryDTO) {
        // 直接用PageHelper进行分页查询 PageNum当前页页码 PageSize当前页条数
        PageHelper.startPage(userPageQueryDTO.getPageNum(), userPageQueryDTO.getPageSize());
        Page<UserVO> page = manageMapper.page(userPageQueryDTO);

        // 为每条数据赋值identify属性
        String identify = userPageQueryDTO.getIdentify();
        page.forEach(userVO -> userVO.setIdentify(identify));

        long total = page.getTotal();
        List<UserVO> records = page.getResult();

        return new PageResult(total, records);
    }

    /**
     * 根据id查询教师和学生信息
     *
     * @param id
     * @param identify
     * @return
     */
    public UserVO getById(Long id, String identify) {
        UserPO userPO = manageMapper.getById(id, identify);

        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userPO, userVO);

        return userVO;
    }

    /**
     * 根据id编辑教师和学生信息
     *
     * @param userDTO
     * @return
     */
    public void updateUser(UserDTO userDTO) {
        UserPO userPO = new UserPO();
        BeanUtils.copyProperties(userDTO, userPO);
        manageMapper.updateUser(userPO);
    }

    /**
     * 根据id删除教师和学生信息
     *
     * @param id
     * @param identify
     * @return
     */
    @Transactional
    public void deleteById(Long id, String identify) {
        // info和detail表都要删除数据
        manageMapper.deleteById(id, identify);
        manageMapper.deleteDetailById(id, identify);
    }

    /**
     * 批量新增教师和学生账号
     *
     * @param excelFile
     * @return
     */
    public void insertBatch(MultipartFile excelFile) throws IOException {
        // 读取excel表格 获取到excel表格的第一页
        XSSFWorkbook excel = new XSSFWorkbook(excelFile.getInputStream());
        XSSFSheet sheet = excel.getSheetAt(0);

        // 准备数据
        List<UserPO> userPOList = new ArrayList<>();
        String identify = sheet.getSheetName();

        // 教师和学生对应的处理逻辑不同
        handleEachRow(sheet, userPOList, identify);

        excel.close();
        manageMapper.insertBatch(userPOList, identify);
    }

    /**
     * 批量禁用教师和学生账号
     *
     * @param excelFile
     * @return
     */
    public void updateBatch(MultipartFile excelFile) throws IOException {
        // 读取excel表格 获取到excel表格的第一页
        XSSFWorkbook excel = new XSSFWorkbook(excelFile.getInputStream());
        XSSFSheet sheet = excel.getSheetAt(0);

        // 准备数据
        List<UserPO> userPOList = new ArrayList<>();
        String identify = sheet.getSheetName();

        // 读取每一行数据 每一行数据为一个UserPO
        for (int i = 0; i < sheet.getLastRowNum(); i++) {
            XSSFRow row = sheet.getRow(i + 1); // 读取每一行数据
            UserPO userPO = new UserPO();
            userPO.setId((long) row.getCell(0).getNumericCellValue()); // 读取该行的每一列数据
            userPOList.add(userPO); // 将每一行的数据UserPO添加到List集合中
        }

        excel.close();
        manageMapper.updateBatch(userPOList, identify);
    }

    private void handleEachRow(XSSFSheet sheet, List<UserPO> userPOList, String identify) {
        // 学生数据还需要获取到班级信息
//        if (StatusConstant.STUDENT.equals(identify)) {
//            // 读取每一行数据 每一行数据为一个UserPO
//            for (int i = 0; i < sheet.getLastRowNum(); i++) {
//                XSSFRow row = sheet.getRow(i + 1); // 读取每一行数据
//
//                // 设置特殊字段的值
//                UserPO userPO = UserPO.builder()
//                        .password(DigestUtils.md5DigestAsHex(DefaultDataConstant.DEFAULT_PASSWORD.getBytes()))
//                        .status(StatusConstant.ENABLE).build();
//
//                // 读取该行的每一列数据
//                userPO.setId((long) row.getCell(0).getNumericCellValue());
//                userPO.setName(row.getCell(1).getStringCellValue());
//                userPO.setSex(row.getCell(2).getStringCellValue());
//                userPO.setCollegeId((int) row.getCell(3).getNumericCellValue());
//                userPO.setMajorId((int) row.getCell(4).getNumericCellValue());
//
//                userPOList.add(userPO); // 将每一行的数据UserPO添加到List集合中
//            }
//        }
//
//        if (StatusConstant.TEACHER.equals(identify)) {
//            // 读取每一行数据 每一行数据为一个UserPO
//            for (int i = 0; i < sheet.getLastRowNum(); i++) {
//                XSSFRow row = sheet.getRow(i + 1); // 读取每一行数据
//
//                // 设置特殊字段的值
//                UserPO userPO = UserPO.builder()
//                        .password(DigestUtils.md5DigestAsHex(DefaultDataConstant.DEFAULT_PASSWORD.getBytes()))
//                        .status(StatusConstant.ENABLE).build();
//
//                // 读取该行的每一列数据
//                userPO.setId((long) row.getCell(0).getNumericCellValue());
//                userPO.setName(row.getCell(1).getStringCellValue());
//                userPO.setSex(row.getCell(2).getStringCellValue());
//                userPO.setCollegeId((int) row.getCell(3).getNumericCellValue());
//
//                userPOList.add(userPO); // 将每一行的数据UserPO添加到List集合中
//            }
//        }
    }

}
